﻿using System.IO;
using Effector.Data;

namespace Effector.ViewModel
{
    /// <summary>
    /// The view model for the logical drives used in data binding.
    /// </summary>
    public class LogicalDriveModel : FileSystemElementModel
    {
        private LogicalDriveElement _logicalDrive;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogicalDriveModel"/> class.
        /// </summary>
        /// <param name="elem">The elem.</param>
        /// <param name="parent">The parent.</param>
        public LogicalDriveModel(LogicalDriveElement elem, FileSystemElementModel parent)
            : base(parent)
        {
            _logicalDrive = elem;
        }

        /// <summary>
        /// Gets the name of the drive.
        /// </summary>
        /// <value>The name of the drive.</value>
        public string DriveName
        {
            get { return _logicalDrive.Name; }
        }

        /// <summary>
        /// Gets the drive element.
        /// </summary>
        /// <value>The drive element.</value>
        public LogicalDriveElement DriveElement
        {
            get { return _logicalDrive; }
        }

        /// <summary>
        /// Loads the children.
        /// </summary>
        protected override void LoadChildren()
        {
            string[] subdirs = Directory.GetDirectories(this.DriveName);
            foreach (string dir in subdirs)
            {
                DirectoryElement delem = new DirectoryElement(new DirectoryInfo(dir));
                DirectoryModel dm = new DirectoryModel(delem, this);
                this.Children.Add(dm as DirectoryModel);
            }
        }

        /// <summary>
        /// Gets the selected.
        /// </summary>
        /// <param name="selectedvalue">The selectedvalue.</param>
        public override void GetSelected(out string selectedvalue)
        {
            selectedvalue = this.DriveName;
        }
    }
}
